﻿--[[
   @brief    run-game popup window
   @author   rudolfzhang
   @version  1.0
   @date     2015-03-27
--]]

popup_wnd_control = {

    init = function()
        local tpf_ui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        -- 此处监听C++端事件
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_LAUNCH_FAIL_AND_REPAIR, "popup_wnd_model.set_repair_guide_wnd");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_LAUNCH_FAIL, "popup_wnd_model.set_failed_to_start_wnd");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_ANTI_PIRACY_ERROR, 
        "popup_wnd_model.set_anti_piracy_wnd");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_UPDATE_FAIL_GAMERUNNING, "popup_wnd_model.set_update_fail_gamerunning_wnd");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_COMMON, "popup_wnd_model.set_common_wnd");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_REQUEST_GAME_ST_FAIL, "popup_wnd_model.restart_tgp_tips");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SHOW_OMD_INSTALL_WARNING, "popup_wnd_model.ShowOMDInstallWarning");
        tpf_ui:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_LOGIN_BEGIN, "popup_wnd_control.on_login_begin");             
        tpf_ui:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_LOGIN_SUCCESS, "popup_wnd_control.on_login_success");  
        tpf_ui:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_LOGIN_FAILED, "popup_wnd_control.on_login_failed");
        tpf_ui:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_GIVEUP_AUTO_LOGIN, "popup_wnd_control.on_giveup_auto_login");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SHOW_LOW_SPEED_TIP, "popup_wnd_control.show_low_speed_tip");
        tpf_ui:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_SCANDANGERS, "popup_wnd_control.on_tas_scandangers");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_ANTI_TGP_NEED_UPDATE, 
        "popup_wnd_model.set_anti_tgp_need_update_wnd");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_ACCOUNT_FROZEN, "popup_wnd_model.ShowFrozenWnd");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SHOW_LAUNCH_DELETED_BRANCH_PROMPT,
            "popup_wnd_model.ShowLaunchDeletedBranchPrompt");
        tpf_ui:AddLuaListener(ierd_tgp.tgp_plugin.RAIL_EXCEPTION_TIPS, "popup_wnd_control.show_rail_exception_tips");
        tpf_ui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_BRANCH_ID_INVALID, "popup_wnd_model.show_invalid_branch_tips");

        event_center.add_listener(login_event.LOGIN_WND_MINIMIZE,   "popup_wnd_control.on_login_wnd_minimize");   
        event_center.add_listener(login_event.LOGIN_WND_RESTORE,   "popup_wnd_control.on_login_wnd_restore");   

        event_center.add_listener(mainframe_event.MAIN_WND_MINIMIZE,   "popup_wnd_control.on_main_wnd_minimize");  
        event_center.add_listener(mainframe_event.MAIN_WND_RESTORE,   "popup_wnd_control.on_main_wnd_restore");
        comm_center.reg_service("Svr_ShowRailGotCardTips", "popup_wnd_control.show_rail_card_tips");

        -- 此处监听web js事件                                                                         
        tpf_ui:RegisterJsEvent("jc_get_login_tips", "popup_wnd_model.handel_jc_get_login_tips"); 
        tpf_ui:RegisterJsEvent("jc_report_poptipsop", "popup_wnd_control.handel_jc_report_poptipsop"); 

        ierd_tgp.tpf_ui.Require('popup_wnd\\model.lua');
        ierd_tgp.tpf_ui.Require('popup_wnd\\view.lua');

        ierd_tgp.tpf_ui.Require('popup_wnd\\web_host.lua');
        ierd_tgp.tpf_ui.Require('popup_wnd\\prelaunch_popup.lua');

        base_utility.log("[popup_wnd_control] init success.");

    end;

    on_login_begin = function(event_id, json_str)   
        base_utility.log("[popup wnd]on_login_begin");  
        popup_wnd_control.change_tas_scandaners_parent(nil);
    end;

    on_login_success = function(event_id, json_str)
        base_utility.log("[popup wnd]on_login_success");
        local info = base_utility.json_to_table(json_str);
        popup_wnd_model.player_uin = info.uin;

        app_launch_annouce_popup.request_config();
    end;
    
    on_tas_scandangers = function (event_id, json_str)
        base_utility.log("[popup wnd]on_tas_scandangers");
        popup_wnd_model.tas_scandaners = true;

        popup_wnd_control.check_show_tas_scandaners();
    end;

    on_main_wnd_show = function(event_id, json_str)  
        base_utility.log("[popup wnd]on_main_wnd_show");
    
        popup_wnd_control.change_tas_scandaners_parent(CID_MAINFRAME_Dialog_MainFrame);
    end;
        
    on_main_wnd_minimize = function ()   
        base_utility.log("[popup wnd]on_main_wnd_minimize");  
        popup_wnd_control.on_tas_scandaners_parent_minimize(CID_MAINFRAME_Dialog_MainFrame);
    end;
       
    on_main_wnd_restore = function ()   
        base_utility.log("[popup wnd]on_main_wnd_restore");                           
        popup_wnd_control.on_tas_scandaners_parent_restore(CID_MAINFRAME_Dialog_MainFrame);
    end;

    on_extendboard_created = function()  
        base_utility.log("[popup wnd]on_extendboard_created"); 
        popup_wnd_control.change_tas_scandaners_parent(CID_EXTENDBOARD_DLG);
    end;

    on_landingpage_created = function()  
        base_utility.log("[popup wnd]on_landingpage_created"); 
        popup_wnd_control.change_tas_scandaners_parent(CID_LANDINGPAGE_DLG);
    end;

    on_giveup_auto_login = function ()  
        base_utility.log("[popup wnd]on_giveup_auto_login"); 
        popup_wnd_control.change_tas_scandaners_parent(CID_LOGIN_DLG);
    end;

    on_login_failed = function ()   
        base_utility.log("[popup wnd]on_login_failed"); 
        popup_wnd_control.change_tas_scandaners_parent(CID_LOGIN_DLG);
    end;

    on_login_wnd_minimize = function ()   
        base_utility.log("[popup wnd]on_login_wnd_minimize");  
        popup_wnd_control.on_tas_scandaners_parent_minimize(CID_LOGIN_DLG);
    end;
       
    on_login_wnd_restore = function ()   
        base_utility.log("[popup wnd]on_login_wnd_restore");  
        popup_wnd_control.on_tas_scandaners_parent_restore(CID_LOGIN_DLG);
    end;

    show_tas_scandangers_tip = function(parent_cid)  
        base_utility.log("[popup wnd]show_tas_scandangers_tip, parent_cid:" .. tostring(parent_cid));  
                                    
        local login_dlg_id = ui_utility.get_ctrl_id_by_cid(parent_cid);
        local param = popup_wnd_model.tas_scandangers_param;
        param.parent_id = login_dlg_id;
        ui_utility.show_tips(param);
        popup_wnd_model.already_show_tas_dangers[parent_cid] = true;
    end;

    show_low_speed_tip = function(event_id, json_str)
        base_utility.log("[popup wnd]show_low_speed_tip, event_id:" .. tostring(event_id) ..
                        "json_str:" .. tostring(json_str));
        local param = base_utility.json_to_table(json_str);
        if param == nil or param.game_id == nil or param.reason == nil then
            return;
        end

        local tip_param = nil;
        if param.reason == ierd_tgp.game_library.ModeTipReason.kPausePreupdate then       
            local parent_id = ui_utility.get_ctrl_id_by_cid(param.op_from_cid);
            tip_param = popup_wnd_model.get_lowspeed_param_pause(param.game_id);
            tip_param.parent_id = parent_id;
        else
            tip_param = popup_wnd_model.get_lowspeed_param_exit(param.game_id);
        end  
       ui_utility.show_tips(tip_param);
       popup_wnd_view.report_click_url(
            ierd_tgp.adapt_for_imports.qos.Qos_url_click_type.kUrlTypeLowspeedTipAppear,
            param.game_id, 0);
    end;

    show_rail_exception_tips = function(event_id, json_str)
        base_utility.log("[popup wnd]show_rail_exception_tips, event_id:" .. tostring(event_id) ..
                        "json_str:" .. tostring(json_str));
        local param = base_utility.json_to_table(json_str);
        if param == nil or param.error_msg == nil then
            return;
        end

        local tip_param = {
            position = 'center',
            title = LoadStr("POP_WND_RAIL_EXCEPTION_TITLE"),
            titleIcon = "",
            icon = "",
            tipsIcon = poptips_icons.kError,
            descTitle = param.error_msg,
            desc = "",
        };
        ui_utility.show_tips(tip_param);
    end;
    
    show_rail_card_tips = function(param)
        base_utility.log("[popup wnd]show_rail_card_tips");
        if param.tips ~= nil then
            local tip_param = {
                position = 'rbottom',
                title = LoadStr("POP_WND_RAIL_CARD_TITLE"),
                icon = "",
                tipsIcon = "",
                width = 360,
                height = 180,
                desc = param.tips,
                modal = false,
                auto_line_gray = "true",
                okBtn = {
                    visible = "true",
                    text = LoadStr("POP_WND_RAIL_CARD_OK"),
                    handle =  function()
                        base_utility.log("[popup wnd]show_rail_card_tips, click okBtn");
                        web_utility.open_link("{\"type\":\"jump_url\", \"id\":\"" .. tostring(param.game_id) .. "\", \"url\":\"" .. param.url .. "\"}");
                        poptips_control.close_tips();
                    end,
                },
                cancelBtn = {
                    visible = "false"
                },
            };
            ui_utility.show_tips(tip_param);
        end
    end;

    show_login_tip = function(event_id, json_str)  
        base_utility.log("[popup wnd]show_login_tip, event_id:" .. tostring(event_id) ..
                        "json_str:" .. tostring(json_str));
        local param = base_utility.json_to_table(json_str);
        if nil == param then  
            base_utility.log("[popup wnd][show_login_tip]param is nil" .. 
                        ", event_id:" .. tostring(event_id) ..
                        ", json_str:" .. tostring(json_str), true);
            return;
        end
        
        -- 把数据先保存下来
        popup_wnd_model.login_tips_ = param;

        local inf = {};
        local win_id = nil;
        inf.title = LoadStr("POP_WND_LOGIN_TIPS_TITLE");
        inf.gameid = 0;
        inf.type = "login_tip";
        inf.width = 740;
        inf.height = 478;
        inf.url = LoadStr("POP_BOX_MARKET_URL");
        inf.manuallyCloseCallback = popup_wnd_control.handle_tip_close;
        inf.js_call_lua = {};
        
        win_id = ui_utility.show_web(inf);
        popup_wnd_model.win2tip[win_id] = param;
    end;

    on_show_login_tips = function (event_id, param)
        base_utility.log("[popup_wnd_control][on_show_login_tips] receive server login tips");
        if popup_wnd_model.game_running_state == 0 then
            popup_wnd_control.show_login_tip(event_id, param);
            base_utility.log("[popup_wnd_control][on_show_login_tips] show login tips");
        elseif popup_wnd_model.game_running_state > 0 then
            local info = base_utility.json_to_table(param);
            if popup_wnd_model.winfo_num >= 3 then
                popup_wnd_model.waiting_tips_info[popup_wnd_model.winfo_num + 1] = info;
                for key, value in pairs(popup_wnd_model.waiting_tips_info) do
                    if  popup_wnd_model.waiting_tips_info[key + 1] ~= nil then
                        popup_wnd_model.waiting_tips_info[key]
                            = popup_wnd_model.waiting_tips_info[key + 1];
                    end
                end
                popup_wnd_model.waiting_tips_info[popup_wnd_model.winfo_num + 1] = nil;
            else
                popup_wnd_model.winfo_num = popup_wnd_model.winfo_num + 1;
                local winfo_num = popup_wnd_model.winfo_num;
                popup_wnd_model.waiting_tips_info[winfo_num] = info; -- 保存tips
            end
            base_utility.log("[popup_wnd_control][on_show_login_tips] save tips "
                .. popup_wnd_model.winfo_num);
        end
    end;

    show_saved_login_tips = function (info_id, time_stamp)
        base_utility.log("[popup_wnd_control][show_saved_login_tips] try to reshow login tips, info_id="
            .. info_id .. " game_running_state=" .. popup_wnd_model.game_running_state);
        if popup_wnd_model.game_running_state == 0 then
            popup_wnd_control.show_login_tip(info_id, base_utility.table_to_json(
                popup_wnd_model.waiting_tips_info[info_id])); -- 重新弹出
            base_utility.log("[popup_wnd_control][show_saved_login_tips] reshow login tips");
            if info_id >= popup_wnd_model.winfo_num then
                popup_wnd_model.waiting_tips_info = {};
                popup_wnd_model.winfo_num = 0;
            end
        end
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            tpfui:RemoveInterval("popup_wnd_control.show_saved_login_tips", info_id);
        end
    end;

    get_game_runing_state = function (event_id, param)
        local table_data = base_utility.json_to_table(param);
        popup_wnd_model.game_running_state = table_data.running_state; -- 同步游戏状态变化
        base_utility.log("[popup_wnd_control][get_game_runing_state] game state is "
            .. popup_wnd_model.game_running_state);
    end;

    game_ended_show_login_tips = function (event_id, param)
        local tips_count = table.getn(popup_wnd_model.waiting_tips_info);
        if tips_count > 0 then
            base_utility.log("[popup_wnd_control][game_ended_show_login_tips] waiting_tips_info, count:"
                .. tips_count);
            local tips_display_idx = 0;
            for key, value in pairs(popup_wnd_model.waiting_tips_info) do
                if tips_display_idx >= 3 then -- 控制tips弹出量
                    return;
                end;
                local tpfui =
                    base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
                tpfui:AddInterval("popup_wnd_control.show_saved_login_tips",
                    tips_display_idx * 2000, key);
                base_utility.log("[popup_wnd_ctl][game_ended_show_login_tips] inform reshow login tips, idx: "
                    .. tips_display_idx .. " key=" .. key);
                tips_display_idx = tips_display_idx + 1;
            end
        end
    end;

    handle_tip_close = function(win_id)            
        base_utility.log("[popup wnd]handle_tip_close, win_id:"  .. tostring(win_id));
        local tip = popup_wnd_model.win2tip[win_id];
        if nil == tip or
            nil == tip.tip_id then  
                base_utility.log("[popup wnd][handle_tip_close]tip NOT FOUND, win_id:"  ..
                    tostring(win_id), true);
                return;
        end

        if true ==  popup_wnd_model.tip_clicked[tip.tip_id] then  
            base_utility.log("[popup wnd][handle_tip_close]tip already cicked" ..
                ", do not report close, win_id:"  .. tostring(win_id));
            return;
        end

        popup_wnd_control.SendPopTipsOP(tip.tip_id, ierd_tgp.tips_mgr.KIND_TIP_CLOSE, 0); 
    end;

    handel_jc_report_poptipsop = function(func_name, json, browser_id)           
        base_utility.log("[popup wnd]handel_jc_report_poptipsop"  ..
            ", func_name:" .. func_name ..
            ", json_str:" .. json);
        if func_name ~= "jc_report_poptipsop" then 
            base_utility.log("[popup wnd][handel_jc_report_poptipsop]invalid func_name"  ..
                ", func_name:" .. func_name ..
                ", json_str:" .. json, true); 
            return;
        end
        local param = base_utility.json_to_table(json);
        if nil == param or
            nil == param.data or
           "string" ~= type(param.data.tip_id) or
           "number" ~= type(param.data.op_kind) or
           "number" ~= type(param.data.page_index) then    
            base_utility.log("[popup wnd][handel_jc_report_poptipsop]param INVALID"  ..
                ", func_name:" .. func_name ..
                ", json_str:" .. json, true); 
                return;
        end
               
        popup_wnd_control.SendPopTipsOP(param.data.tip_id, param.data.op_kind, param.data.page_index);   

        local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");  

        local rsp_json = base_utility.table_to_json(rsp_table);
        web_utility.lua_call_js_by_id_ex(param.callback_func, rsp_json, browser_id);

        if ierd_tgp.tips_mgr.KIND_TIP_CLICK == param.data.op_kind then
            popup_wnd_model.tip_clicked[param.data.tip_id] = true;
        end
    end;

    SendPopTipsOP = function(tip_id, op_kind, page_index) 

        local tips_mgr =
            base_utility.get_component_adapter("ITIPS_mgr",
                "ierd_tgp.tips_mgr.GetTipsMgrModule();");
        if nil == tips_mgr then
            return;
        end

        tips_mgr:SendPopTipsOP(tip_id, op_kind, page_index); 
    end;

    change_tas_scandaners_parent = function(parent_cid) 
        base_utility.log("[popup wnd]change_tas_scandaners_parent " .. tostring(parent_cid));
        popup_wnd_model.tas_scandaners_parent_cid =  parent_cid;
        popup_wnd_control.check_show_tas_scandaners();
    end;

    check_show_tas_scandaners = function()    
        base_utility.log("[popup wnd]check_show_tas_scandaners, parent_cid:" ..
            tostring(popup_wnd_model.tas_scandaners_parent_cid));
                
        if not popup_wnd_model.tas_scandaners then  
            base_utility.log("[popup wnd][check_show_tas_scandaners]no dangers");
            return;
        end

        if not popup_wnd_model.tas_scandaners_parent_cid then  
            base_utility.log("[popup wnd][check_show_tas_scandaners]parent_cid is nil");
            return;
        end

        if popup_wnd_model.wnd_minimize[popup_wnd_model.tas_scandaners_parent_cid] then 
            base_utility.log("[popup wnd][check_show_tas_scandaners]parent wnd is minimized");
            return;
        end;

        if popup_wnd_model.already_show_tas_dangers[popup_wnd_model.tas_scandaners_parent_cid] then   
            base_utility.log("[popup wnd][check_show_tas_scandaners]tas dangers has show");
            return;
        end;

        popup_wnd_control.show_tas_scandangers_tip(popup_wnd_model.tas_scandaners_parent_cid);
    end;

    on_tas_scandaners_parent_minimize = function(parent_cid)  
        base_utility.log("[popup wnd]on_tas_scandaners_parent_minimize " .. tostring(parent_cid));
        popup_wnd_model.wnd_minimize[parent_cid] = true;
    end;

    on_tas_scandaners_parent_restore = function(parent_cid) 
        base_utility.log("[popup wnd]on_tas_scandaners_parent_restore " .. tostring(parent_cid)); 
        popup_wnd_model.wnd_minimize[parent_cid] = false;
        popup_wnd_control.check_show_tas_scandaners();
    end;
}

popup_wnd_control.init();